/*
 * Copyright (c) 2019. seven studio. @chengdu
 * seven studio in chengdu by lb
 * this project for snmp tool
 */

package com.xlx.openLink.service;

import com.xlx.openLink.dao.SpeedMapper;
import com.xlx.openLink.entity.pojo.Port;
import com.xlx.openLink.entity.pojo.Speed;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Slf4j
@Service
public class SpeedService {


    @Autowired

    private SpeedMapper speedMapper;

    @Transactional
    public int updateBatch(List<Speed> list) {
        int loopCount = list.size() / 100;
        int left = list.size() % 100;
        for (int i = 0; i < loopCount; i++) {
            int fromIndex = i * 100;
            speedMapper.updateBatch(list.subList(fromIndex, fromIndex + 100));
        }
        if (left > 0) {
            int fromIndex = loopCount * 100;
            speedMapper.updateBatch(list.subList(fromIndex, fromIndex + left));
        }
        return 1;
    }

    /**
     * 批量更新操作
     *
     * @param list
     * @return
     */
    @Transactional
    public int insertBatch(List<Speed> list) {
        int loopCount = list.size() / 100;
        int left = list.size() % 100;
        for (int i = 0; i < loopCount; i++) {
            int fromIndex = i * 100;
            speedMapper.insertBatch(list.subList(fromIndex, fromIndex + 100));
        }
        if (left > 0) {
            int fromIndex = loopCount * 100;
            speedMapper.insertBatch(list.subList(fromIndex, fromIndex + left));
        }
        return 1;
    }

    public List<Speed> selectListList(String pId) {
        return speedMapper.selectListList(pId);
    }

}
